/**
 * 
 */
package com.antilia.infomaresme.pages.edit.news;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxFallbackDefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;

import com.antilia.infomaresme.InfomaresmeApplication;
import com.antilia.infomaresme.data.dataobjects.Item;
import com.antilia.infomaresme.data.dataobjects.ItemNews;
import com.antilia.infomaresme.data.dataobjects.VisibilityStatus;
import com.antilia.infomaresme.pages.RoundButtonLinkPanel;
import com.antilia.infomaresme.provider.ItemNewssDataProvider;
import com.antilia.infomaresme.resources.InfoModalWindow;

/**
 * @author Ernesto
 *
 */
public class NewsPanel extends Panel {

	private static final long serialVersionUID = 1L;

	private ItemNewssDataProvider dataProvider;

	private ModalWindow modal;
	
	private AddEditNewsPanel panel;
	
	private AjaxFallbackDefaultDataTable<ItemNews> items;
	/**
	 * @param id
	 */
	public NewsPanel(String id, final IModel<Item> model) {
		super(id,model);
		
		modal = new InfoModalWindow("modal");		
		add(modal);
		
		RoundButtonLinkPanel addNews = new RoundButtonLinkPanel("addNews", new ResourceModel("actions.AddNews")) {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				panel= new AddEditNewsPanel(modal.getContentId(), model, null) {
					
					private static final long serialVersionUID = 1L;

					protected void onSave(AjaxRequestTarget target, org.apache.wicket.markup.html.form.Form<?> form) {
						panel.getNews().setDate(new Date());
						InfomaresmeApplication.getQAApplication().getiItemNewsDao().save(panel.getNews());
						modal.close(target);
						target.add(NewsPanel.this.items);
					};
				};
				modal.setContent(panel);
				modal.show(target);
				modal.setTitle(new ResourceModel("editNews.title"));
				modal.setInitialWidth(850);
				modal.setInitialHeight(620);
				modal.setResizable(true);
			}
		};
		List<IColumn<ItemNews>> columns = new ArrayList<IColumn<ItemNews>>();	
		columns.add(new PropertyColumn<ItemNews>(Model.of(""), null, "x") {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void populateItem(
					org.apache.wicket.markup.repeater.Item<ICellPopulator<ItemNews>> item,
					String componentId, IModel<ItemNews> rowModel) {
				item.add(new EditDeleteLinkPanel(componentId, rowModel) {
					
					private static final long serialVersionUID = 1L;

					@Override
					public void onDelete(AjaxRequestTarget target,
							IModel<ItemNews> model) {
						
					}
					
					@Override
					public void onEdit(AjaxRequestTarget target,
							final IModel<ItemNews> model1) {
						modal.setContent(new AddEditNewsPanel(modal.getContentId(), model, model1.getObject()) {
							
							private static final long serialVersionUID = 1L;

							@Override
							protected void onSave(AjaxRequestTarget target,
									Form<?> form) {
								InfomaresmeApplication.getQAApplication().getiItemNewsDao().save(model1.getObject());
								modal.close(target);
								target.add(NewsPanel.this.items);
							}
						});
						modal.show(target);
						modal.setTitle(new ResourceModel("editNews.title"));
						modal.setInitialWidth(850);
						modal.setInitialHeight(620);
						modal.setResizable(true);
					}
				});
			}
		});
		columns.add(new PropertyColumn<ItemNews>(new ResourceModel("date"), null, "date"));
		columns.add(new PropertyColumn<ItemNews>(new ResourceModel("title"), null, "title"));
				
		
 		dataProvider = new ItemNewssDataProvider() {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected VisibilityStatus getStatus() {
				return null;
			}
			
			@Override
			public Item getItem() {
				return model.getObject();
			}
		};
		add(addNews);
		items  = new AjaxFallbackDefaultDataTable<ItemNews>("news", columns, dataProvider, 10);
		items.setOutputMarkupId(true);
		addOrReplace(items);
	}

}
